<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Life Search V1.0Alpha</title>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8">
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/engine.js"></script>
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/util.js"></script>
<script type='text/javascript' src="${pageContext.request.contextPath}/dwr/interface/searchService.js"></script>
<script language="javascript">
function doSearch(type){

	var result = document.getElementById("result");
	var pagingdiv = document.getElementById('paging');
	result.innerHTML="";
	pagingdiv.innerHTML = "";

	if (type != 'paging') {
		var startindexinput = document.getElementById('startindex');
		startindexinput.value = "1";
	}	
	
	request = {startindex:1, query:""};
	DWRUtil.getValues(request);

	searchService.getSearchResults(request, fillPage);
}
function fillPage(data){

	var list = data.results;
	var resultdiv = document.getElementById('result');
	var pagingdiv = document.getElementById('paging');

	resultdiv.innerHTML = "";
	pagingdiv.innerHTML = "";

	if (list.length == 0) {
		resultdiv.innerHTML = "<span>Sorry, we can't find what you want...</span>";
		return;
	}
	
	for(var i=0; i<list.length; i++) {
		var ele = document.createElement('div');
		ele.setAttribute('id','info' + list[i]);
		ele.innerHTML = "<span>loading..</span>";
		resultdiv.appendChild(ele);
		searchService.getSearchResultById(list[i], fillDetailResult);
	}
	
	minpage = data.minpage;
	maxpage = data.maxpage;
	startindex = data.startindex;
	hasnext = data.hasnext;
	
	if (minpage != 1) {
		var link = document.createElement('a');
		link.setAttribute("href","javascript:paging('" + ((minpage-11)*10+1) + "')");
		link.innerHTML = "前10页<<";
		pagingdiv.appendChild(link);
	}
	
	for (var j=minpage; j <=maxpage; j++)
	{
		if ((j-1)*10+1 != startindex)
		{
			var link = document.createElement('a');
			link.setAttribute("href","javascript:paging('" + ((j-1)*10+1) + "')");
			link.innerHTML = "第" + j + "页";
			pagingdiv.appendChild(link);
		}
		else {
			pagingdiv.innerHTML += ("第" + j + "页");
		}
	}
	
	if (hasnext == 1) {
		var link = document.createElement('a');
		link.setAttribute("href","javascript:paging('" + (maxpage*10+1) + "')");
		link.innerHTML = ">>后10页";
		pagingdiv.appendChild(link);
	}
	
}
function fillDetailResult(record) {
	var result = document.getElementById('info' + record.id);
	result.innerHTML="<table border='0' cellpadding='0' cellspacing='0' width=500><tr><td width='120'></td><td><a href='detail.jsp?id="
	                  + record.id + 
	                  "' target='_blank'><font color=#C60A00 size='2'>"
	                  + "<font size='2'>名称：</font>" + record.name + 
	                  "</font><br>" + "<font size='2'>地点：" + record.category + "</font><br><font color=#C60A00 size='2'>"
	                  + record.summary + "......" + "</font><br><a href='" + record.url + "'>"
	                  + "<font size=2>" + record.url + "</font>" + "</a><br><font size='2' color=green>索引时间:</font>"
	                  + "<font size=2 color=green>" + record.indexCreateTime + "</font>"
	                  + "</td></tr></table>";
}
function paging(newindex) {
	document.getElementById('startindex').value = newindex;
	doSearch('paging');
}


function handlekey(){

	if (document.getElementById('query').value == '')
		return;
	var intkey = -1;
	if(window.event) {
		intKey = event.keyCode;
		if(intKey == 13){
			doSearch('');
		}
	}
}
</script>

</head>
<body>
<input type="hidden" name="startindex" id="startindex" value="1">
<!-- 这是搜索栏 -->
<div id="searchbar">
	<table align="center">
		<tr align="center">
			<td>
				<br />
				<br />
				<br><font face="STKaiti" size=36 color="purple"><strong>Life Search V1.0Alpha</strong></font></br>
				<br />
			</td>
		</tr>
		<tr align="center">
			<td><input size="50" type="text" name="query" id="query" value="烤翅"  onkeydown="handlekey()">
			<input type="button" value="Search"  id="search" onclick="javascript:doSearch('')"></td>
		</tr>
	</table>
</div>

<hr>

<!-- 这是结果栏 -->
<div id="result">
</div>

<hr>

<!-- 这是分页栏 -->
<div id="paging">

</div>


</body>
</html>